﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExcelToChart
{
    public partial class Form_CombinedSheet : Form
    {
        DataTable date_number_datatable;
        public Form_CombinedSheet()
        {
            InitializeComponent();
        }

        public Form_CombinedSheet(DataTable dt)
        {
            date_number_datatable = dt;
            InitializeComponent();
        }

        public static DataTable getTestDataTable()
        {
            DataTable tdt = new DataTable();

            DataColumn dateDataColumn = tdt.Columns.Add("Date");
            DataColumn countDataColumn = tdt.Columns.Add("Count");
            Random rd = new Random();

            for (int i = 1; i <= 31; i++)
            {
                DataRow dr = tdt.NewRow();
                dr[0] = string.Format("2021-05-{0:D2}", i);
                dr[1] = rd.Next(1000, 3000);
                tdt.Rows.Add(dr);
            }

            for (int i = 1; i <= 30; i++)
            {
                DataRow dr = tdt.NewRow();
                dr[0] = string.Format("2021-06-{0:D2}", i);
                dr[1] = rd.Next(1000, 3000);
                tdt.Rows.Add(dr);
            }

            return tdt;
        }

        public static DataTable datatableDateFormat(DataTable dt)
        {
            DataTable dtf = dt.Copy();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DateTime date;
                try
                {
                    date = DateTime.ParseExact(dt.Rows[i][0].ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.ToString() + "TODO: 需要手动修正错误");
                    continue;
                }

                dtf.Rows[i][0] = date;
            }

            return dtf;
        }

        private void button_week_report_Click(object sender, EventArgs e)
        {
            DataTable dtf = datatableDateFormat(date_number_datatable);

            var keys = new DataColumn[1];
            DataTable dtWeekCombine = new DataTable();
            DataColumn column;

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "年-周";
            dtWeekCombine.Columns.Add(column);
            keys[0] = column;

            column = new DataColumn();
            column.DataType = Type.GetType("System.Int32");
            column.ColumnName = "数量";
            dtWeekCombine.Columns.Add(column);
            //keys[1] = column;

            dtWeekCombine.PrimaryKey = keys;

            for (int i =0; i< dtf.Rows.Count; i++)
            {
                int value = int.Parse(dtf.Rows[i][1].ToString());
                DateTime dateOfRow = DateTime.Parse(dtf.Rows[i][0].ToString());

                int year = dateOfRow.Year;
                GregorianCalendar gc = new GregorianCalendar();
                int weekOfYear = gc.GetWeekOfYear(dateOfRow, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                //Console.WriteLine(weekOfYear);
                //Console.ReadLine();

                //string key = year.ToString() + "-" + weekOfYear.ToString();
                string key = string.Format("{0}-{1}", year, weekOfYear);
                DataRow dr = dtWeekCombine.Rows.Find(key);
                if(dr != null)
                {
                    //已经有这个年-周的key了.  插入
                    dr[1] = (int)dr[1] + value;
                }
                else
                {
                    //新的一周, 新建一个row
                    DataRow newRow = dtWeekCombine.NewRow();
                    newRow[0] = key;
                    newRow[1] = value;
                    dtWeekCombine.Rows.Add(newRow);
                }
            }

            dataGridView2.Columns.Clear();
            dataGridView2.DataSource = dtWeekCombine;
        }

        private void button_month_report_Click(object sender, EventArgs e)
        {
            DataTable dtf = datatableDateFormat(date_number_datatable);

            var keys = new DataColumn[1];
            DataTable dtMonthCombine = new DataTable();
            DataColumn column;

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "年-月";
            dtMonthCombine.Columns.Add(column);
            keys[0] = column;

            column = new DataColumn();
            column.DataType = Type.GetType("System.Int32");
            column.ColumnName = "数量";
            dtMonthCombine.Columns.Add(column);
            //keys[1] = column;

            dtMonthCombine.PrimaryKey = keys;

            for (int i = 0; i < dtf.Rows.Count; i++)
            {
                int value = int.Parse(dtf.Rows[i][1].ToString());
                DateTime dateOfRow = DateTime.Parse(dtf.Rows[i][0].ToString());

                int year = dateOfRow.Year;
                int month = dateOfRow.Month;
                //GregorianCalendar gc = new GregorianCalendar();
                //int weekOfYear = gc.GetWeekOfYear(dateOfRow, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                //Console.WriteLine(weekOfYear);
                //Console.ReadLine();

                string key = string.Format("{0}-{1}", year, month);
                DataRow dr = dtMonthCombine.Rows.Find(key);
                if (dr != null)
                {
                    //已经有这个年-周的key了.  插入
                    dr[1] = (int)dr[1] + value;
                }
                else
                {
                    //新的一周, 新建一个row
                    DataRow newRow = dtMonthCombine.NewRow();
                    newRow[0] = key;
                    newRow[1] = value;
                    dtMonthCombine.Rows.Add(newRow);
                }
            }

            dataGridView3.Columns.Clear();
            dataGridView3.DataSource = dtMonthCombine;
        }

        private void ToolStripMenuItem_load_test_data_Click(object sender, EventArgs e)
        {
            // 拿到原始数据, 日期为字符串
            date_number_datatable = getTestDataTable();

            foreach (DataRow dr in date_number_datatable.Rows)
            {
                Console.WriteLine(dr.ToString());
            }

            dataGridView1.Columns.Clear();
            dataGridView1.DataSource = date_number_datatable;
        }
    }
}
